package edu.temple.mobile.parsing.helpers;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import edu.temple.mobile.parsing.SeptaAlerts;

public class RSSFeedXMLHelper extends DefaultHandler{
	
	private boolean inChannel;
	private boolean inItem;
	private boolean inDescription;
	private boolean inPubDate;
	private boolean inLink;
	
	private Integer rowCount = 0;

	
	Boolean currentElement = false;
	String currentValue = null;
	private SeptaAlerts septaAlerts = new SeptaAlerts();

	public SeptaAlerts getSeptaAlerts() {
		return this.septaAlerts;
	}
	
	@Override
    public void startDocument() throws SAXException {
            // Do some startup if needed
    }

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		//Gets placemark ID
		if (localName.equals("channel")){
			this.inChannel = true;
		}else if (localName.equals("item")){
			this.inItem = true;
		}else if (localName.equals("description")){
			this.inDescription = true;
		}else if (localName.equals("pubDate")){
			this.inPubDate = true;
		}else if (localName.equals("link")){
			this.inLink = true;
		}
	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if (localName.equals("channel")){
			this.inChannel = false;
		}else if (localName.equals("item")){
			this.inItem = false;
		}else if (localName.equals("description")){
			this.inDescription = false;
		}else if (localName.equals("pubDate")){
			this.inPubDate = false;
		}else if (localName.equals("link")){
			this.inLink = false;
		}

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(this.inDescription){
			String desc = new String(ch, start, length);
            septaAlerts.setDescription(desc);
		}
		
		if(this.inPubDate){
			String pDate = new String(ch, start, length);
            septaAlerts.setDescription(pDate);
		}
		
		if(this.inLink){
			String pDate = new String(ch, start, length);
            septaAlerts.setDescription(pDate);
		}
	}
	
	@Override
    public void endDocument() throws SAXException {
            // Do some finishing work if needed
    }

}
